<?php

namespace App\Http\Controllers\Backstage\Setting;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Http\Request;

//系统设置模块
class SettingController extends Controller
{
    //获取系统设置信息
    public function getSettingInfo(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $info = DB::table('setting_info')->get();
        if ($info) {
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }
    //保存站点信息
    public function saveSettingInfo(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $settingdata = $request->input('settingdata');

        foreach ($settingdata as $key => $value) {
            $map['sid'] = $value['sid'];
            $date['value'] = $value['value'];
            $info[$key] = DB::table('setting_info')->where($map)->update($date);
        }
        if (!empty($info)) {
            $data = success();
        } else {

            $data = fail();
        }
        return $data;
    }
    //获取首页概览数据
    public function getoverview(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $date['articlenum'] = DB::table('article')->count();
        $date['adsnum'] = DB::table('ads')->count();
        $date['messagenum'] = DB::table('message')->count();
        if ($date) {
            $data = success();
            $data['data'] = $date;
        } else {
            $data = fail();
        }
        return $data;
    }

    //相关设置
    public function getSetting(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['setting_id'] = (int) $request->query('setting_id');
        $info = DB::table('setting')->where($map)->first();
        if ($info) {
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }

    //保存相关设置
    public function updateSetting(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['setting_id'] = (int) $request->input('setting_id');
        $data=json_decode($request->input('settingdata'));
        // $data[1]->value=Crypt::encryptString($data[1]->value);
        $settingdata['value'] =  json_encode($data);

        // foreach ($settingdata as $key => $value) {
        //     $map['sid'] = $value['sid'];
        //     $date['value'] = $value['value'];
        $info = DB::table('setting')->where($map)->update($settingdata);
        // }
        if (!empty($info)) {
            $data = success();
        } else {

            $data = fail();
        }
        return $data;
    }

    //首页获取分类状态
    public function getHomeCateStatus(Request $request){
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['status_id']=2;
        $info=DB::table('status')->where($map)->first();
        if($info){
            $data=success();
            $data['data']=$info;
        }else{
            $data=fail();
        }
        return $data;
    }
    //首页设置分类状态
    public function setHomeCateStatus(Request $request){
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['status_id']=2;
        $date['status']=(int)$request->input('status');
        $msg=DB::table('status')->where($map)->update($date);
        if($msg){
            $data=success();
        }else{
            $data=fail();
        }
        return $data;
    }
}
